package com.toman.ui.operation.enterprise.line;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Adpter.LineEditTypeAdapter;
import com.toman.Interfaces.BaseUrl;
import com.toman.R;
import com.toman.widget.PullZoomListView;
import com.toman.Util.TimeUtil;
import com.toman.Util.ToastUtil;
import com.toman.bean.LineEditTypeBean;
import com.toman.toolbar.ToolBarActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;
import okhttp3.Response;

public class LineEditTypeActivity extends ToolBarActivity implements PullZoomListView.OnRefreshListener {
    int id;
    @BindView(R.id.lv)
    PullZoomListView lv;
    @BindView(R.id.tv_none)
    TextView tvNone;
    private LineEditTypeAdapter adapter;
    List<LineEditTypeBean> list = new ArrayList<LineEditTypeBean>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_edit_type);
        ButterKnife.bind(this);

        initView();
        typeHistory();
//        initList();
    }

    private void initList() {
        for (int i = 0; i < 20; i++) {
            LineEditTypeBean bean = new LineEditTypeBean(i, "TYPE-" + i, "小米", "2017-" + i, i + "");
            list.add(bean);
        }
        adapter.refreshData(list);
    }

    private void initView() {
        adapter = new LineEditTypeAdapter(this);
        lv.setAdapter(adapter);
        lv.setOnRefreshListener(this);

        id = getIntent().getIntExtra("id", -1);
        initToolBar("型号历史(" + getIntent().getStringExtra("name") + ")", getResources().getColor(R.color.c1));

    }

    public void typeHistory() {
        String token = myApplication.getSp().getString("tokenId", "");
        OkHttpUtils
                .post(BaseUrl.productTypeHistory)
                .params("token", token)
                .params("id", id + "")
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", " 型号历史: " + result);
                        try {
                            JSONObject js = new JSONObject(result);
                            int code = js.getInt("code");
                            if (code == 1) {
                                JSONObject da = js.getJSONObject("data");
                                JSONArray array = null;
                                try {
                                    array = da.getJSONArray("rows");
                                } catch (Exception e) {
                                    tvNone.setVisibility(View.VISIBLE);
                                    e.printStackTrace();
                                }
                                if (da.getString("total").equals("0")) {
                                    tvNone.setVisibility(View.VISIBLE);

                                } else {
                                    for (int i = 0; i < array.length(); i++) {
                                        JSONObject us = array.getJSONObject(i);
                                        String time = us.getString("createtime");
                                        LineEditTypeBean bean = new LineEditTypeBean(us.getInt("id"), us.getString("product_type"), us.getString("operator_user_name"), TimeUtil.TimeStamp2Date(time, "yyyy-MM-dd HH:mm:ss"), TimeUtil.formatTime(us.getLong("changetime")));
                                        list.add(bean);
                                    }


                                    adapter.refreshData(list);
                                }


                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void onRefresh() {
        ToastUtil.print("ok");
    }
}
